import Vue from 'vue'
import Router from 'vue-router'
import store from './store'
import Member from './router/member'
import Order from './router/order'
import NProgress from '@/plugins/nprogress.js'
// 免token白名单
const whiteList = [ '/login', '/registry' ] // no redirect whitelist
Vue.use(Router)
const routerInstance = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      meta: {},
      component: () => import('@/apps/member/views/Auth/Login.vue')
    },
    {
      path: '/register',
      name: 'Register',
      meta: {},
      component: () => import('@/apps/member/views/Auth/Register.vue')
    },
    {
      path: '/',
      name: 'Index',
      redirect: { name: 'MemberIndex' },
      component: () => import('@/apps/member/Layout/Layout.vue'),
      children: [
        Member,
        Order
      ]
    }
  ],
  scrollBehavior (to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return { x: 0, y: 0 }
  }
})
routerInstance.beforeEach((to, from, next) => {
  NProgress.start()
  // 检测是否登录 和 白名单
  if (whiteList.indexOf(to.path) === -1 && !store.getters.isLogin) {
    next({ name: 'Login' })
  } else {
    next()
  }
})
routerInstance.afterEach((to, from) => {
  NProgress.done() // finish progress bar
})
export default routerInstance
